from django.db import models
from django.contrib.auth import get_user_model
from apps.books.models import Book

User = get_user_model()


class Comment(models.Model):
    """评论表"""
    COMMENT_STATUS_CHOICES = [
        ('pending', '待审核'),
        ('approved', '已审核'),
        ('rejected', '已拒绝'),
        ('deleted', '已删除'),
    ]

    book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comments')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='replies')
    
    content = models.TextField(verbose_name='评论内容')
    rating = models.IntegerField(choices=[(i, i) for i in range(1, 6)], verbose_name='评分')
    status = models.CharField(max_length=20, choices=COMMENT_STATUS_CHOICES, default='pending', verbose_name='审核状态')
    
    # 审核信息
    reviewed_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='reviewed_comments')
    reviewed_at = models.DateTimeField(null=True, blank=True, verbose_name='审核时间')
    review_note = models.TextField(blank=True, verbose_name='审核备注')
    
    # 点赞统计
    like_count = models.IntegerField(default=0, verbose_name='点赞数')
    dislike_count = models.IntegerField(default=0, verbose_name='点踩数')
    
    # 管理员回复
    admin_reply = models.TextField(blank=True, verbose_name='管理员回复')
    replied_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='replied_comments')
    replied_at = models.DateTimeField(null=True, blank=True, verbose_name='回复时间')
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'comments'
        verbose_name = '评论'
        verbose_name_plural = '评论'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.email} 对 {self.book.title} 的评论"


class CommentLike(models.Model):
    """评论点赞"""
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='likes')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment_likes')
    is_like = models.BooleanField(default=True)  # True for like, False for dislike
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'comment_likes'
        verbose_name = '评论点赞'
        verbose_name_plural = '评论点赞'
        unique_together = ['comment', 'user']

    def __str__(self):
        action = '点赞' if self.is_like else '点踩'
        return f"{self.user.email} {action} {self.comment.id}"


class CommentImage(models.Model):
    """评论图片"""
    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='images')
    image = models.ImageField(upload_to='comments/images/')
    alt_text = models.CharField(max_length=100, blank=True)
    sort_order = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'comment_images'
        verbose_name = '评论图片'
        verbose_name_plural = '评论图片'
        ordering = ['sort_order']

    def __str__(self):
        return f"评论 {self.comment.id} 的图片"


class CommentReport(models.Model):
    """评论举报"""
    REPORT_REASON_CHOICES = [
        ('spam', '垃圾信息'),
        ('inappropriate', '不当内容'),
        ('harassment', '骚扰'),
        ('copyright', '版权侵权'),
        ('other', '其他'),
    ]

    comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='reports')
    reporter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='comment_reports')
    reason = models.CharField(max_length=20, choices=REPORT_REASON_CHOICES, verbose_name='举报原因')
    description = models.TextField(blank=True, verbose_name='详细描述')
    status = models.CharField(max_length=20, choices=[
        ('pending', '待处理'),
        ('resolved', '已处理'),
        ('dismissed', '已驳回'),
    ], default='pending', verbose_name='处理状态')
    
    handled_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True, related_name='handled_reports')
    handled_at = models.DateTimeField(null=True, blank=True, verbose_name='处理时间')
    handled_note = models.TextField(blank=True, verbose_name='处理备注')
    
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'comment_reports'
        verbose_name = '评论举报'
        verbose_name_plural = '评论举报'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.reporter.email} 举报评论 {self.comment.id}"
